package org.gosdt.ew.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.gosdt.ew.model.Topic;


public class TopicForm extends BaseForm {

	/**
	 * form for adding new topics in DB
	 */
	private static final long serialVersionUID = 1L;
	
	private JLabel jlbNewTopic = new JLabel();
	private JTextField jtfTopicName = new JTextField(50);
	private JLabel jlbErrors = new JLabel();
	
	private JButton jbtSave = new JButton();
	private JButton jbtCancel = new JButton();
	
	private JPanel jpnAvailableActions = new JPanel(); // any common buttons
	private JPanel jpnAvailableSettings = new JPanel(null); // all other thing

	/**
	 * create topic form for adding new topics in DB
	 * @param parent - main form
	 */
	public TopicForm(JFrame parent){
		super(parent, "add new topic");
		doInitForm();
		this.setVisible(true);
	}
	
	@Override
	/**
	 * Should't use outside of class
	 * Initialize form elements. Sets size and positions.
	 */
	public void doInitForm() {
		int fWidth = this.getWidth();
		int large = fWidth - 10;
		int magic = 350; // count of pixel for good button any
		
		setLabelsName();
		
		this.add(jpnAvailableActions, BorderLayout.SOUTH);
		this.add(jpnAvailableSettings, BorderLayout.CENTER);
		
		jlbErrors.setBounds(5,5, large, 25);
		jlbNewTopic.setBounds(5,30, large, 25);
		jtfTopicName.setBounds(5,60, large, 25);
		
		jpnAvailableSettings.add(jlbNewTopic);
		jpnAvailableSettings.add(jtfTopicName);
		jpnAvailableSettings.add(jlbErrors);
		
		if (fWidth <= magic){
			jbtCancel.setPreferredSize(new Dimension((int)(fWidth * 0.30), 25));
			jbtSave.setPreferredSize(new Dimension((int)(fWidth * 0.30), 25));
			jpnAvailableActions.setLayout(new FlowLayout(FlowLayout.CENTER, (int)(fWidth * 0.025), 0));
		}
		
		jpnAvailableActions.add(jbtCancel);
		jpnAvailableActions.add(jbtSave);
		
		setButtonListeners();
	}

	/**
	 * remove all listeners and close form
	 */
	private void doCloseForm(){
		jbtCancel.removeActionListener(jbtCancel.getActionListeners()[0]);
		jbtSave.removeActionListener(jbtSave.getActionListeners()[0]);
		this.dispose();
	}
	
	@Override
	/**
	 * sould't use outside of class
	 * add actions to components
	 */
	public void setButtonListeners() {
		jbtCancel.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				doCloseForm();
			}
		});
		
		jbtSave.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (!jtfTopicName.getText().toLowerCase().trim().equals("")){
					new Topic(jtfTopicName.getText().toLowerCase().trim(), myParent.getProfile().getStudyLanguage());
					doCloseForm();
				}
				else {
					jlbErrors.setText("Тема не может быть пустой");
				}
			}
		});
					
	}

	@Override
	/**
	 * sould't use outside of class
	 * set text to components.
	 */
	public void setLabelsName() {
		this.setTitle(myParent.getTitle() + " - Add new topic");
		jlbNewTopic.setText("Название новой темы");		
		jlbErrors.setForeground(Color.RED);
		jbtSave.setText("Сохранить");
		jbtCancel.setText("Закрыть");
	}

}
